﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebEntities;
using WebEntities.Models.WebInfo;

namespace com.subb.WebInfo
{
    public class WI_AdvItemDAL
    {
        public List<WI_AdvItem> GetAllAdvItems(int start, int limit, out int ttlCount)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.WI_AdvItem.OrderBy(c => c.AdvID);
                ttlCount = query.Count();
                List<WI_AdvItem> adItemList = query.Skip(start).Take(limit).ToList();
                return adItemList;
            }
        }

        public List<WI_AdvItem> GetAllAdvItems(int suiteId, int start, int limit, out int ttlCount)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.WI_AdvItem.Where(c => c.SuiteID == suiteId).OrderBy(c => c.AdvID);
                ttlCount = query.Count();
                List<WI_AdvItem>  adItemList = query.Skip(start).Take(limit).ToList();
                return adItemList;
            }
        }

        public List<WI_AdvItem> GetAllAdvItemsByUI(int suiteId)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                List<WI_AdvItem> adItemList = new List<WI_AdvItem>();
                DateTime nowTime = DateTime.Now;
                var query = ctx.WI_AdvItem.Where(c => c.SuiteID == suiteId && c.BTime <= nowTime && c.ETime >= nowTime);
                if (query.Count() > 0)
                {
                    adItemList = query.ToList();
                }
                return adItemList;
            }
        }

        public WI_AdvItem GetAdvItem(int advId)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                WI_AdvItem aditem = ctx.WI_AdvItem.SingleOrDefault(c => c.AdvID == advId);
                return aditem;
            }
        }

        public int CreateAdvItem(AdvItem v_adItem)
        {
            int id = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_AdvItem wiadItem = new WI_AdvItem
                {
                    AdvTitle = v_adItem.AdvTitle,
                    AdvContent = v_adItem.AdvContent,
                    AdvType = v_adItem.AdvType,
                    AppearRate = v_adItem.AppearRate,
                    SuiteID = v_adItem.SuiteID,
                    CreateTime = DateTime.Now,
                    Creator = v_adItem.Creator,
                    UpdateTime = DateTime.Now,
                    Modifier = v_adItem.Creator,
                    PicUrl = v_adItem.PicUrl,
                    PicLink = v_adItem.PicLink,
                    AdvWidth = v_adItem.AdvWidth,
                    AdvHeight = v_adItem.AdvHeight,
                    BTime = v_adItem.BTime,
                    ETime = v_adItem.ETime
                };
                ctx.WI_AdvItem.AddObject(wiadItem);
                ctx.SaveChanges();
                id = wiadItem.AdvID;
            }
            return id;
        }

        public bool UpdateAdvItem(int advId, AdvItem v_adItem)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_AdvItem wiaItem = ctx.WI_AdvItem.SingleOrDefault(c => c.AdvID == advId);
                if (wiaItem != null)
                {
                    wiaItem.AdvTitle = v_adItem.AdvTitle;
                    wiaItem.AdvContent = v_adItem.AdvContent;
                    wiaItem.AdvType = v_adItem.AdvType;
                    wiaItem.AppearRate = v_adItem.AppearRate;
                    wiaItem.UpdateTime = DateTime.Now;
                    wiaItem.Modifier = v_adItem.Modifier;
                    wiaItem.PicUrl = v_adItem.PicUrl;
                    wiaItem.PicLink = v_adItem.PicLink;
                    wiaItem.AdvWidth = v_adItem.AdvWidth;
                    wiaItem.AdvHeight = v_adItem.AdvHeight;
                    wiaItem.BTime = v_adItem.BTime;
                    wiaItem.ETime = v_adItem.ETime;
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public bool RemoveAdvItem(int advId)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_AdvItem wiaItem = ctx.WI_AdvItem.SingleOrDefault(c => c.AdvID == advId);
                if (wiaItem != null)
                {
                    ctx.WI_AdvItem.DeleteObject(wiaItem);
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }
    }
}
